package com.fingard.dsp.bank.directbank.ceb02;

import com.fingard.dsp.bank.directbank.bankInterface.IPrivateSaleMsg;
import com.fingard.dsp.bank.reqrespobj.privatesale.ReqPrivateSaleMsgQry;
import com.fingard.dsp.bank.reqrespobj.privatesale.RetAtsPrivateSaleDetail;
import com.fingard.dsp.bank.reqrespobj.privatesale.RetAtsPrivateSaleMsg;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class CEB02PrivateSaleMsg extends CEB02Base implements IPrivateSaleMsg{

	@Override
	public RetAtsPrivateSaleMsg getPrivateSaleMsg(ReqPrivateSaleMsgQry reqInfo) throws Exception {
		RetAtsPrivateSaleMsg retAts = new RetAtsPrivateSaleMsg(reqInfo.reqHeader);
		retAts.writeStart();
		String tmpRespCode = "";
        String tmpRespInfo = "";
		int PAGE = 0;
		int BISHU = 50;
        try {
        	for(int i=0;i<=PAGE;i++) {
            CEB02Header bankHeader = new CEB02Header();
            //银行获取相关信息
            bankHeader.FTranCode = "G054";
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入通用的头部节点
            writeCommonHeader_3(xmlWriter, bankHeader);  
            //写入body
            xmlWriter.writeStartElement("Body");
            xmlWriter.writeElementString("CHANNEL_CODE", bankFront.org);//接入机构号
            xmlWriter.writeElementString("DEALER_CODE",reqInfo.DealerCode);//经销商编号          
            xmlWriter.writeElementString("DISCOVERY_SALE_DATE", reqInfo.DiscoverySaleDate);//私售日期
            xmlWriter.writeElementString("QISHI",i*BISHU+"");//起始笔数
            xmlWriter.writeElementString("BISHU",BISHU+"");//查询笔数
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(),bankHeader);//发送银行
            if(tmpStrRet[0].length() == 0){
                StringBuilder tmpSbMsg = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if(tmpBkReader != null){
                	String tmpRetCode = tmpBkReader.getFirstText("/Out/Head/ErrCode");
                	String tmpRetMsg = tmpBkReader.getFirstText("/Out/Body/ErrorInfo");
                        if (tmpRetCode.length()==0) {
                        	tmpRespCode = "0";
                        	tmpRespInfo = "获取成功";
                            //翻页处理
                            if(PAGE == 0) {
                                int Z_BISHU = Integer.valueOf(tmpBkReader.getFirstText("/Out/Body/Z_BISHU")); 
                                int count = Z_BISHU/BISHU;
                                if(count>0) {
                                	if(Z_BISHU%BISHU==0) {
                                		PAGE = count - 1;
                                	} else {
                                	PAGE = count;
                                	}
                                }
                            }
                            while (tmpBkReader.read("/Out/Body/Frame")) {
                            	RetAtsPrivateSaleDetail tmpRetDetail = new RetAtsPrivateSaleDetail();
                                tmpRetDetail.ID = tmpBkReader.readedXElement.getSingleText("ID");
                                tmpRetDetail.DEALER_CODE = tmpBkReader.readedXElement.getSingleText("DEALER_CODE");
                                tmpRetDetail.DEALER_NAME = tmpBkReader.readedXElement.getSingleText("DEALER_NAME");
                                String date = tmpBkReader.readedXElement.getSingleText("DISCOVERY_SALE_DATE");
                                if(date!=null && date.contains("-")){
                                    date = date.replaceAll("-","");
                                }
                                tmpRetDetail.DISCOVERY_SALE_DATE = date;
                                tmpRetDetail.CAR_SERIES_CN = tmpBkReader.readedXElement.getSingleText("CAR_SERIES_CN");
                                tmpRetDetail.CAR_TYPE_CODE = tmpBkReader.readedXElement.getSingleText("CAR_TYPE_CODE");
                                tmpRetDetail.VIN = tmpBkReader.readedXElement.getSingleText("VIN");
                                tmpRetDetail.CAR_PRICE = tmpBkReader.readedXElement.getSingleText("CAR_PRICE");
                                tmpRetDetail.SALE_PLACE = tmpBkReader.readedXElement.getSingleText("SALE_PLACE");
                                tmpRetDetail.REDEMPTION_DATE = tmpBkReader.readedXElement.getSingleText("REDEMPTION_DATE");
                                tmpRetDetail.STATUS = tmpBkReader.readedXElement.getSingleText("STATUS");
                                tmpRetDetail.RESERVE4 = tmpBkReader.readedXElement.getSingleText("RESERVE4");
                                tmpRetDetail.RESERVE5 = tmpBkReader.readedXElement.getSingleText("RESERVE5");
                                tmpRetDetail.RESERVE6 = tmpBkReader.readedXElement.getSingleText("RESERVE6");
                                retAts.writeRetDetail(tmpRetDetail);
                            }
                        } else {
                        	tmpRespCode = "-1";
                            tmpRespInfo = tmpRetMsg;
                            break ;
                        }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpSbMsg.toString();
                    break ;
                }
            } else {
                tmpRespCode = "-1";
                tmpRespInfo = tmpStrRet[1];
                break ;
            }
          }
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        retAts.writeRespCode(tmpRespCode);
        retAts.writeRespInfo(tmpRespInfo);
        retAts.writeEnd();
        return retAts;
    }

}
